package com.scqkxx.gamebox.activity.register;

import android.content.ContentValues;
import android.content.Intent;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import com.avos.avoscloud.AVCloud;
import com.avos.avoscloud.AVException;
import com.avos.avoscloud.FunctionCallback;
import com.scqkxx.gamebox.R;

import com.scqkxx.gamebox.activity.activities.utils.ActivityUtil;
import com.scqkxx.gamebox.databinding.ActivityRegisterEmailBinding;
import com.scqkxx.gamebox.utils.VerifyUtils;
import com.scqkzqtz.base.base.BaseActivity;
import com.scqkzqtz.base.library.utils.PreferenceUtils;

import java.util.HashMap;
import java.util.Map;

public class RegisterEmailActivity extends BaseActivity<ActivityRegisterEmailBinding> {

    @Override
    public int setLayoutId() {
        return R.layout.activity_register_email;
    }

    @Override
    public void init() {
        binding.mTitleView.setOnTitleClickListener((int titleType) -> finish());
        binding.userPhone.setOnClickListener((View view) -> {
            startActivity(new Intent(this, RegisterPhoneActivity.class));
        });

        CountDownTimer countDownTimer = new CountDownTimer(3000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {

            }

            @Override
            public void onFinish() {
                binding.llError.setVisibility(View.INVISIBLE);
            }
        };

        binding.btNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username = binding.etName.getText().toString();
                if (TextUtils.isEmpty(username)) {
                    showError("请输入正确的邮箱",countDownTimer);
                    return;
                }

                if (!VerifyUtils.isEmail(username)) {
                    showError("请输入正确的邮箱",countDownTimer);
                    return;
                }

                PreferenceUtils.commitString("register_email", username);
                Map<String, Object> map = new HashMap<>();
                map.put("username", username);
                AVCloud.callFunctionInBackground("sendCodeByEmail", map, new FunctionCallback<Map<String, Object>>() {
                    @Override
                    public void done(Map<String, Object> object, AVException e) {
                        //{msg=邮件发送成功, data={}, error_code=0}
                        Log.d("sendCodeByEmail", object.toString());
                        String msg = object.get("msg") + "";
                        String error_code = map.get("error_code") + "";
                        if (e == null && !"0".equals(error_code)) {
                            ContentValues contentValues = new ContentValues();
                            contentValues.put("EmailUrl",username);
                            ActivityUtil.switchTo(RegisterEmailActivity.this, RegisterCheckCodeActivity.class,contentValues);
                        } else {
                            showError(msg,countDownTimer);
                        }
                    }
                });
            }
        });
    }

    private void showError(String msg, CountDownTimer countDownTimer) {
        if (countDownTimer == null) return;
        countDownTimer.start();
        binding.errorMsg.setText(msg);
        binding.llError.setVisibility(View.VISIBLE);
    }
}
